<template>
    <el-dialog :title="coushareid==-1?'新增营销活动':'修改营销活动'" :visible.sync="open" :before-close="close" append-to-body>
        <CouLists @getcousid="getcousid" />
        <el-form :model="f" ref="CouponForm">
            <el-form-item label="领劵条件" :label-width="formLabelWidth">
                满<el-input class="inputw" v-model="f.num" auto-complete="off"></el-input>人
            </el-form-item>
            <el-form-item label="指定优惠券" :label-width="formLabelWidth">
                <el-input class="inputw" v-model="couponId" auto-complete="off" disabled></el-input>
                <el-button @click="getgood()">选择指定优惠券</el-button>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="close()">取 消</el-button>
            <el-button type="primary" @click="save()">确 定</el-button>
        </div>
    </el-dialog>
</template>
<script>
import moment from 'moment'
import CouLists from 'components/Coupon/CouLists'
export default {
    data(){
        return{
            open:false,
            f:{},
            couponId:0,
            id:-1,
            marketingId:-1
        }
    },
    computed:{
        getstate(){
            return this.$store.state.couruleditclass
        },
        getchange(){
            return this.$store.state.couruledit
        }
    },
    watch:{
        getstate(v){//获取弹窗打开状态
            this.open=v
        },
        getchange(v){//获取入参并对表单赋值
            if(v.id==undefined){
                console.log("isNEW")
                this.marketingId=v.marketingId
            }else{
                console.log("isEDIT")
                this.couponId=v.couponId
                this.id=v.id
                this.f.num=v.num
                this.marketingId=v.marketingId
            } 
        }
    },
    components:{
        CouLists
    },
    methods:{
        close(){//关闭并清空数据
            this.id=-1
            this.f={}
            this.couponId=0
            this.marketingId=-1
            this.$store.state.couruleditclass=false
            this.$store.state.couruledit={}
        },
        getcousid(m){//获取优惠券id
            this.couponId=''
            this.couponId=m
        },
        getgood(){//新增
            this.$store.commit('ChangeCouLists')
        },
        save(){//保存
            var _this=this
            var pp=new URLSearchParams()
            if(!this.couponId){
                this.$message.error("请选择关联优惠券")
                return
            }
            pp.append('couponId',this.couponId)
            if(!this.f.num){
                this.$message.error("请填写领劵条件")
                return
            }
            pp.append('num',this.f.num)
            pp.append('marketingId',this.marketingId)

            if(this.id==-1){//新增
                axios.get('/saveShareCouponRel.do',{params:pp}).then(r=>{
                    if(r.data.code==200){
                        _this.$message.success("添加成功")
                        _this.$store.state.courulesdialog=false
                        _this.close()
                    }else{
                        _this.$message.error(r.data.message)
                    }
                })
            }else{
                pp.append('id',this.id)
                axios.get('/updateShareCouponRel.do',{params:pp}).then(r=>{
                    if(r.data.code==200){
                        _this.$message.success("修改成功")
                        _this.$store.state.courulesdialog=false
                        _this.close()
                    }else{
                        _this.$message.error(r.data.message)
                    }
                })
            }
        }
    }
}
</script>
<style scoped>
.inputw{
    width: 100px; 
    margin-right: 10px;
}
</style>
